import json

import aiohttp
import scrapy
from scrapy.http.request.json_request import JsonRequest

from urllib.parse import urlencode

class RequestMethodSpider(scrapy.Spider):
    name = "request_method"

    async def start(self):
        # yield scrapy.Request(f"http://httpbin.org/get?name=qiku&{urlencode({
        #     "id": 101,
        #     "age": 10
        # })}", callback=self.parse_get, headers={
        #     "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/142.0.0.0 Safari/537.36"
        # }, method="GET")


        # yield scrapy.Request(f"http://httpbin.org/post?name=qiku", method="POST", body=json.dumps({
        #     "age": "10",
        # }), headers={
        #     "Content-Type": "application/json",
        # })

        # yield scrapy.Request(f"http://httpbin.org/post?name=qiku", method="POST", body=urlencode({
        #     "age": "10",
        # }), headers={
        #     "Content-Type": "application/x-www-form-urlencoded",
        # })


        # yield scrapy.FormRequest(f"http://httpbin.org/post?name=qiku", formdata={
        #     "age": "10"
        # })

        yield JsonRequest(f"http://httpbin.org/post?name=qiku", data=json.dumps({
            "age": 10
        }), method='POST')

    def parse_get(self, response):
        print(response.url, response.json())

    def parse(self, response):
        print(response.json())
